package com.jweb.framework.security.crypto.keystore;

import java.security.KeyStore;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;

import com.jweb.framework.security.crypto.common.EKeyStoreType;

/**
 * 修改人： Thomas
 * 修改内容：新增 
 * 类说明：JKS工厂
 */
public class KeyStoreFactoryBean implements FactoryBean<KeyStore>, InitializingBean {
	
	protected Resource location;
	protected String password;
	protected EKeyStoreType type = EKeyStoreType.JKS;
	protected KeyStore keyStore ;
	
	public KeyStore getObject() throws Exception {
		return this.keyStore;
	}
	
	public Class<?> getObjectType() {
		return KeyStore.class;
	}
	
	public boolean isSingleton(){
		return true ;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		this.keyStore = KeyStore.getInstance(this.type.toString());
		this.keyStore.load(this.location.getInputStream(), this.password.toCharArray());
	}
	
	public void setLocation(Resource resource){
		this.location = resource ;
	}
	
	public void setPassword(String password){
		this.password = password;
	}
	
	public void setType(EKeyStoreType type){
		this.type = type;
	}
	
}
